package com.swgk.core.util;

import android.annotation.SuppressLint;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * Description:
 * Author zhengkewen
 * Time 2018/1/10
 */
public class MDateUtil {

    public enum Format {
        FORMART1("yyyy-MM-dd HH:mm:ss"),
        FORMART2("yyyy-M-d"),
        FORMART3("H:m"),
        FORMART4("MM-dd"),
        FORMART5("yyyy-MM-dd"),
        FORMART6("HH:mm");

        public String name;

        Format(String name) {
            this.name = name;
        }
    }

    public static String getCurrentDate(Format format) {
        SimpleDateFormat sFormat = new SimpleDateFormat();
        sFormat.applyPattern(format.name);
        return sFormat.format(Calendar.getInstance().getTime());
    }

    public static long getFormatTimestamp(String format) {
        long timestamp = 0;
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
            String temp = Format.FORMART5.name + Format.FORMART6.name;
            simpleDateFormat.applyPattern(temp);
            Date date = simpleDateFormat.parse(format);
            timestamp = date.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return timestamp;
    }

    public static long getTimestampByCustom(String value, String format) {
        long timestamp = 0;
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
            simpleDateFormat.applyPattern(format);
            Date date = simpleDateFormat.parse(value);
            timestamp = date.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return timestamp;
    }

    public static String getMothFromDate(String date) {
        date = date.substring(5, 7);
        if (date.substring(0, 1).equals("0")) {
            date = date.substring(1);
        }
        return date;
    }

    public static String getByTimestamp(long time, String format) {
        String timestamp = "";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
        simpleDateFormat.applyPattern(format);
        Date date = new Date(time);
        timestamp = simpleDateFormat.format(date);
        return timestamp;
    }

    public static String getDate(String s) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        long lt = new Long(s);
        Date date = new Date(lt);
        return simpleDateFormat.format(date);
    }

    public static String getYMDHSDate(String time) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        long lt = new Long(time);
        Date date = new Date(lt);
        return simpleDateFormat.format(date);
    }

    /**
     * 获取与当前日期时间差值
     *
     * @param time
     * @return
     */
    public static String getDateDiff(String time) {
        long lt = new Long(time);
        long l = System.currentTimeMillis() - lt;
        int days = (int) l / (1000 * 60 * 60 * 24);
        int hours = (int) (l % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
        int minutes = (int) (l % (1000 * 60 * 60)) / (1000 * 60);
//        int seconds = (int) (l % (1000 * 60)) / 1000;
        if (days > 0) {
            return days + "天前";
        } else if (hours > 0) {
            return hours + "小时前";
        } else if (minutes > 0) {
            return minutes + "分钟前";
        } else {
            return "刚刚更新";
        }
    }

    public static String getDateTimeFromMillisecond(Long millisecond, String pattern) {
        @SuppressLint("SimpleDateFormat")
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        Date date = new Date(millisecond);
        return simpleDateFormat.format(date);
    }

    public static String getStartAndEndDate(Long startTime, Long endTime) {
        return timestampToDate(startTime) + "~" + timestampToDate(endTime);
    }

    public static String timestampToDate(long time) {
        if (time < 10000000000L) {
            time = time * 1000;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
        return sdf.format(new Date(Long.parseLong(String.valueOf(time))));
    }

    public static String getStartAndEndDate1(Long startTime, Long endTime) {
        return timestampToDate1(startTime) + "~" + timestampToDate1(endTime);
    }

    public static String timestampToDate1(long time) {
        if (time < 10000000000L) {
            time = time * 1000;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd", Locale.CHINA);
        return sdf.format(new Date(Long.parseLong(String.valueOf(time))));
    }
}
